package com.gx.smart.building.ui.setting.fragment;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.navigation.Navigation;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.SettingFragmentBinding;
import com.gx.smart.building.ui.common.util.AppUserInfoUtil;
import com.gx.smart.building.ui.login.activity.LoginActivity;
import com.gx.smart.building.ui.setting.util.DataCleanManager;
import com.gx.smart.building.ui.setting.viewmodel.SettingViewModel;
import com.mylhyl.circledialog.CircleDialog;
import com.orhanobut.logger.Logger;

public class SettingFragment extends BaseMVVMFragmentV2<SettingViewModel> {

    private SettingFragmentBinding dataBinding;
    private String cacheSize;

    @Override
    public int onBindLayout() {
        return R.layout.setting_fragment;
    }

    @Override
    public String getToolbarTitle() {
        return "设置";
    }

    @Override
    protected Class<SettingViewModel> onBindViewModel() {
        return SettingViewModel.class;
    }

    @Override
    public void initContentView(ViewGroup root) {
        dataBinding = DataBindingUtil.inflate(getLayoutInflater(), onBindLayout(), root, true);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setSettingFragment(this);
    }


    public void cacheClear(View v) {
        String content = "缓存大小为" + cacheSize + "，确定要清理缓存吗？";
        new CircleDialog.Builder()
                .setTitle("温馨提示")
                .setText(content)
                .setPositive("确定", v1 -> clearCache())
                .setNegative("取消", null)
                .show(getActivity().getSupportFragmentManager());
    }

    public void about(View v) {
        Navigation.findNavController(v).navigate(R.id.action_settingFragment_to_aboutFragment);
    }

    public void support(View v) {
        Navigation.findNavController(v).navigate(R.id.action_settingFragment_to_supportFragment);
    }

    public void logout(View v) {
        new CircleDialog.Builder()
                .setTitle("登出")
                .setText("您确定要退出登录吗？")
                .setPositive("确定", v1 -> logout())
                .setNegative("取消", null)
                .show(getActivity().getSupportFragmentManager());
    }

    @Override
    protected void initData() {
        try {
            cacheSize = DataCleanManager.getTotalCacheSize(getActivity());
            dataBinding.setCacheSize(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(e, "TotalCacheSize");
        }
    }


    private void logout() {
        AppUserInfoUtil.clearLogin();
        AppUserInfoUtil.clearAppLogin();
        ToastUtils.showLong("登出成功");
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        ActivityUtils.startActivity(intent);
    }

    private void clearCache() {
        try {
            long totalSize = DataCleanManager.getTotalSize(getActivity());
            DataCleanManager.clearAllCache(getActivity());
            long newTotalSize = DataCleanManager.getTotalSize(getActivity());
            String deleteSize =
                    DataCleanManager.getFormatSize(((double) totalSize) - ((double) newTotalSize));
            ToastUtils.showLong("清除缓存空间大小：" + deleteSize);
            dataBinding.setCacheSize(DataCleanManager.getFormatSize((double) newTotalSize));
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(e, "clearCache");
        }

    }
}